<?php
include_once 'protoweb_controller.php';

/**--------------------------------------------------------------------------
 * Please, don't touch this file.
 *
 * If you need to custom this Class, you can edit permissions.php
 * file overwrite and define new methods
 *---------------------------------------------------------------------------
 */

/**
 * @package Protoweb
 * @subpackage controller
 */
class Permissions_protoweb extends Protoweb_controller {

  public function __construct() {
    parent::__construct();
  } // end of method __construct()



  public function edit($id=0) {
    $data["permission"] = new Permission($id);

    if ($this->input->post("posting") == 1)
      $data = $this->edit_save($data["permission"]->id);

    // To one relationships
        
    $users = new User();
    $data["users"] = $users->all_to_asoc_array("id","username");
    $data["users_selected"] = $data["permission"]->user->id;
        
    $clients = new Client();
    $data["clients"] = $clients->all_to_asoc_array("id","name");
    $data["clients_selected"] = $data["permission"]->client->id;
    
    // To many relationships
    
    $this->smarty_parser->view('permission_edit.html', $data);
  } // end of method edit($id=0)


  private function edit_save($id) {
    $data["error"] = "";
    $data["permission"] = new Permission($id);

          // Assign value to field. If value is empty, we will assign NULL
      $data["permission"]->access = $this->input->post("access");
      if ($data["permission"]->access == "") $data["permission"]->access = NULL;
      
      // files for upload
      /*
      if ($data["permission"]->access->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(access)) $data["permission"]->access = $fileupload.clean_file_name(access);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
    
    //saving attributes
    if(! $data["permission"]->save())
      $data["error"] .= $data["permission"]->error->string;

    //saving to one relationships
    
    $user = new User($this->input->post("user"));
    if (! $data["permission"]->save(array($user)))
      $data["error"] .= $data["permission"]->error->string;
    
    $client = new Client($this->input->post("client"));
    if (! $data["permission"]->save(array($client)))
      $data["error"] .= $data["permission"]->error->string;
    
    //to many relationships
    
    redirect(base_url()."permissions/edit/".$data["permission"]->id);
  } // end of method edit_save($id)


  public function delete($id) {
    $data["error"] = "";
    $data["permission"] = new Permission($id);

    //delete
    if(! $data["permission"]->delete())
      $data["error"] .= $data["permission"]->error->string;

    redirect(base_url()."permissions/edit/");
  } // end of method delete($id)

    
} // class Permissions_protoweb

/* End of file permissions_protoweb.php */
/* Location: ./system/application/controllers/permissions_protoweb.php */
